<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class client extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->is_logged_in();
        $this->load->model('client/client_model');
        $this->load->model('user/user_model');
        $this->load->model('client/client_config_model');
    }

    public function index() {
        $clients = $this->client_model->get();
        $this->_data['clients'] = $clients;
        $this->_data['users'] = $this->user_model->find_all(); //var_dump($this->_data['users']);exit;
        $this->config->load('site', TRUE);
        $this->_data['prefecture'] = $this->config->item('prefecture_all', 'site');
        $this->_data['prefecture'] += $this->config->item('prefecture', 'site');
        $this->load->admintemplate('list', $this->_data);
    }

    public function search() {
        $this->config->load('form_validate', TRUE);
        $this->config->load('site', TRUE);
        $prefecture_cd = $this->input->post('prefecture_cd', TRUE);
        $client_name = $this->input->post('client_name', TRUE);
        $this->_data['clients'] = $this->client_model->getAllBySearch($client_name, $prefecture_cd);
        $this->_data['prefecture'] = $this->config->item('prefecture_all', 'site');
        $this->_data['prefecture'] += $this->config->item('prefecture', 'site');
        $this->load->admintemplate('list', $this->_data);
    }

    public function add() {
        $this->load->library('form_validation');
        $this->config->load('form_validate', TRUE);
        $this->form_validation->set_rules($this->config->item('client', 'form_validate'));

        if ($this->form_validation->run() == FALSE) {
            $this->config->load('site', TRUE);
            $this->_data['prefecture'] = $this->config->item('prefecture', 'site');
            $this->_data['content_location'] = $this->config->item('content_location', 'site');
            $this->load->admintemplate('add', $this->_data);
        } else {
            $params['user_nm'] = $this->input->post('user_nm', TRUE);
            $params['password'] = $this->encode_pwd($this->input->post('password', TRUE));
            $params['name'] = $this->input->post('name', TRUE);
            $params['prefecture_cd'] = $this->input->post('prefecture_cd', TRUE);
            $this->db->trans_begin();
            $client_id = $this->client_model->insert($params);
            $params_client['id'] = $client_id;
            $params_client['delete_content'] = $this->input->post('delete_content', TRUE);
            $params_client['delete_server'] = $this->input->post('delete_server', TRUE);
            $params_client['timing_connect'] = $this->input->post('timing_connect', TRUE);
            //$params_client['storage_size'] = $this->input->post('storage_size', TRUE);
            $this->client_config_model->insert($params_client);
            if ($this->db->trans_status() === false) {
                $this->system_log->log('SQL Failed: ' . $this->db->last_query() . ' - ' . $this->db->_error_message());
                $this->db->trans_rollback();
                $error = array('error' => 'Insert Client Failed!');
                show_error($error, 500);
            } else {
                $this->db->trans_commit();
            }
            redirect('client/index');
        }
    }

    public function edit($id) {

        $this->_data['client'] = $this->client_model->find_id($id);
        $this->_data['client_config'] = $this->client_config_model->find_id($id);

        $this->load->library('form_validation');
        $this->config->load('form_validate', TRUE);
        $this->form_validation->set_rules($this->config->item('clientEdit', 'form_validate'));

        if ($this->form_validation->run() == FALSE) {
            $this->config->load('site', TRUE);
            $this->_data['prefecture'] = $this->config->item('prefecture', 'site');
            $this->_data['content_location'] = $this->config->item('content_location', 'site');
            $this->load->admintemplate('edit', $this->_data);
        } else {
            $pwd = $this->input->post('password', TRUE);
            if ($pwd != '') {
                $params['password'] = $this->encode_pwd($pwd);
            }
            $params['name'] = $this->input->post('name', TRUE);
            $params['prefecture_cd'] = $this->input->post('prefecture_cd', TRUE);

            $this->db->trans_begin();
            $this->client_model->update($id, $params);

            $params_client['delete_content'] = $this->input->post('delete_content', TRUE);
            $params_client['delete_server'] = $this->input->post('delete_server', TRUE);
            $params_client['timing_connect'] = $this->input->post('timing_connect', TRUE);
            //$params_client['storage_size'] = $this->input->post('storage_size', TRUE);

            $this->client_config_model->update($id, $params_client);
            if ($this->db->trans_status() === false) {
                $this->system_log->log('SQL Failed: ' . $this->db->last_query() . ' - ' . $this->db->_error_message());
                $this->db->trans_rollback();
                $error = array('error' => 'Update Client Failed!');
                show_error($error, 500);
            } else {
                $this->db->trans_commit();
            }

            redirect('client/index');
        }
    }

    public function delete() {
        $id = $this->input->post('id');
        $this->db->trans_start();
        // Delete client
        $delete = (boolean) $this->client_model->delete($id);
        if ($delete == false) {
            echo 0;
            $this->db->trans_complete();
            die;
        }
        // Delete client's config
        $this->client_config_model->delete($id);
        $this->db->trans_complete();
        if ($this->db->trans_status() !== false) {
            echo 1;
        } else {
            echo 0;
            $this->system_log->log('SQL Failed: ' . $this->db->last_query() . ' - ' . $this->db->_error_message());
        }
        die;
    }

    public function listplaylist() {
        $client_id = $this->input->post('client_id', 0);
        $playlists = $this->client_model->getPlaylist($client_id);
        $this->_data['playlists'] = $playlists;
        $this->load->view('listplaylist', $this->_data);
    }

}
